package com.zkw.gro.cloure.demo

/**
 * Created by Administrator on 2016/4/9 0009.
 */
class CallingClosureMain {
    /**
     * 两种不同的闭包调用方式
     */
    def simple(){
        def adder = {x,y -> return x+y}
        assert adder(4,3) == 7
        assert adder.call(4,3) == 7
    }
    /**
     * 从一个方法内部调用闭包
     * @param args
     */
    def benchmark(repeat,Closure worker){
        def start = System.currentTimeMillis()
        repeat.times{worker(it)}
        def stop = System.currentTimeMillis()
        return stop - start
    }

    public static void main(String[] args) {
        CallingClosureMain callingClosureMain = new CallingClosureMain()
//        callingClosureMain.simple()
        def slow = callingClosureMain.benchmark(10000){(int) it/2}
        def fast = callingClosureMain.benchmark(10000){it.intdiv(2)}
        assert fast * 5 <slow

    }
}
